package master;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.Timer;

public class Rechner extends JPanel {

	private static final long serialVersionUID = 1L;
	private int number;
	private String name;
	private JButton infoButton;
	private Kasse main;
	private Stopuhr timer;
	private Timer refreshTimer;
	private JLabel rechnerZeit;
	private ArtikelListe artikelListe;
	private RechnerButtonLeiste buttonLeiste;
	private Abrechnung abrechnung = null;
	private Font font = new Font("Arial",0,20);

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	public Rechner() {
		this(0, null);
	}

	public Rechner(int number, Kasse main) {
		this(number, null, main);
	}

	public Rechner(int number, String name, Kasse main) {
		this.setMain(main);
		this.setNumber(number);
		this.timer = new Stopuhr(this);
		if (name == null) {
			this.name = "Rechner " + number;
		} else {
			this.name = name;
		}
		JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);
		this.infoButton = new JButton("Info");
		this.infoButton.setFont(new Font("Arial", 0, 30));
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.setBorder(BorderFactory.createEtchedBorder());

		this.setRechnerZeit(new JLabel(getTime()));
		JLabel rechnername = new JLabel(this.name);
		rechnername.setFont(new Font("Arial", Font.BOLD, 20));
		this.add(new AlignBox(rechnername, true));
		this.add(separator);
		infoButton.setPreferredSize(new Dimension(150, 150));

		this.add(new StretchBox(infoButton));

		this.add(Box.createVerticalGlue());
		this.buttonLeiste = new RechnerButtonLeiste(this);
		this.add(buttonLeiste);
		this.setBackground(Color.GREEN);
		this.infoButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new RechnerInfoWindow(getRechner());
			}
		});
		this.refreshTimer = new Timer(50, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				refresh();
			}
		});
		artikelListe = new ArtikelListe(getMain().getTaskBox());
		refreshTimer.start();
	}

	public RechnerButtonLeiste getButtonLeiste() {
		return buttonLeiste;
	}

	public void setButtonLeiste(RechnerButtonLeiste buttonLeiste) {
		this.buttonLeiste = buttonLeiste;
	}

	protected void refresh() {
		if (this.isTaken())
			this.infoButton.setText(getPaymentMoneyString());
		if (!this.isTaken())
			this.infoButton.setText("");
		if(!(timer.isActive()) && this.isTaken()) this.stop(false);
		this.infoButton.setBackground(this.getBackground());
	}

	protected Rechner getRechner() {
		return this;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public Kasse getMain() {
		return main;
	}

	public void setMain(Kasse main) {
		this.main = main;
	}

	public void start() {
		if (this.timer.start())
			this.setBackground(Color.RED);
	}

	public void stop() {
		if (this.timer.stop()) {
			if (this.abrechnung == null) {
				this.setBackground(Color.YELLOW);
			} else {
				this.abrechnung.dispose();
			}
			this.abrechnung = new Abrechnung(this, timer);
		}
	}

	public void stop(boolean trash) {
		this.timer.stop();
		this.setBackground(Color.YELLOW);
		buttonLeiste.getStopButton().setText("ABRECHNUNG");
		buttonLeiste.getStopButton().setBackground(Color.MAGENTA);
		buttonLeiste.stop();
	}

	public ArtikelListe getArtikel() {
		return this.artikelListe;
	}

	public String getTime() {
		return this.timer.toString() == "00:00:00" ? "" : this.timer.toString();
	}

	public JLabel getRechnerZeit() {
		return rechnerZeit;
	}

	public void setRechnerZeit(JLabel rechnerZeit) {
		this.rechnerZeit = rechnerZeit;
	}

	public void resume() {
		if (this.timer.resume())
			this.setBackground(Color.RED);
	}

	public Data getData() {
		return getMain().getData();
	}

	public long getPaymentTime() {
		return timer.getTime2Pay();
	}

	public String getPaymentMoneyString() {
		return main.getData().moneyToString(
				getPaymentMoney() + getArtikel().getSumme());
	}

	public double getPaymentMoney() {
		return main.getData().time2Money(getPaymentTime());
	}

	public void reset() {
		timer.delete();
		artikelListe = new ArtikelListe(getMain().getTaskBox());
		this.buttonLeiste.reset();
		this.abrechnung = null;
		this.setBackground(Color.GREEN);
	}

	public AlignBox getAbrechnung() {
		JPanel resultPanel = new JPanel();
		resultPanel.setLayout(new BoxLayout(resultPanel, BoxLayout.PAGE_AXIS));
		resultPanel.add(new AlignBox(new JLabel(getPaymentMoneyString())));
		return new AlignBox(resultPanel);
	}

	public boolean isTaken() {
		return (timer.isActive()) || (getArtikel().getSumme() > 0);
	}

}
